gusucode.com > 遍历和查找外部程序 Tree-View 中的项目C++源码程序 > 遍历和查找外部程序 Tree-View 中的项目/treeviewdemo/TV_Demo_SeqSearch/TV_Demo_SeqSearchDlg.cpp

    // TV_Demo_SeqSearchDlg.cpp : implementation file
//

#include "stdafx.h"
#include "TV_Demo_SeqSearch.h"
#include "TV_Demo_SeqSearchDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();
	
	// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA
	
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL
	
	// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTV_Demo_SeqSearchDlg dialog

CTV_Demo_SeqSearchDlg::CTV_Demo_SeqSearchDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTV_Demo_SeqSearchDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTV_Demo_SeqSearchDlg)
	// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTV_Demo_SeqSearchDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTV_Demo_SeqSearchDlg)
	DDX_Control(pDX, IDC_ITEM_TEXT, m_item_text);
	DDX_Control(pDX, IDC_HANDLE_TARGET, m_handle_target);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTV_Demo_SeqSearchDlg, CDialog)
//{{AFX_MSG_MAP(CTV_Demo_SeqSearchDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_GO, OnGo)
ON_BN_CLICKED(IDC_ABOUT, OnAbout)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTV_Demo_SeqSearchDlg message handlers

BOOL CTV_Demo_SeqSearchDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	
	// Add "About..." menu item to system menu.
	
	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);
	
	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}
	
	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CTV_Demo_SeqSearchDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CTV_Demo_SeqSearchDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting
		
		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
		
		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;
		
		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CTV_Demo_SeqSearchDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CTV_Demo_SeqSearchDlg::OnGo() 
{
	// TODO: Add your control notification handler code here
	
	/*	Tree-View Control_Demo_SeqSearch 1.0 版
	*	版权所有 (C) 2006 天津 赵春生
	*	2006.08.28
	*	http://timw.yeah.net
	*	http://timw.126.com
	*	本程序能按用户指定的项目名称顺序查找TV控件中的项目。
	*	代码在Win2000P+SP4 + VC6+SP6测试通过。
	*/
	
	
	HWND hwnd;
	TV_ITEM tvitem;
	
	char hex_str[16]="0";
	char *s;
	int digit,error=0;
	unsigned long dec_sum;
	
	TV_ITEM *ptvitem;
	char ItemBuf[512]="",*pItem;
	DWORD PID;
	HANDLE hProcess;
	char str_item_text[32]="";//要查找的项目名称,即:item text
	int Bingo=0;//是否找到匹配项目,默认否
	
	
	
	//对输入的数据做简单的验证,无错误后转换成10进制存放在dec_sum中。	
	m_handle_target.GetWindowText (hex_str,9);
	
	if(strlen(hex_str)!=0)//输入数据不得为空
	{
		s=hex_str;
		
		while (isspace(*s))	s++;
		
		for (dec_sum=0; isxdigit(*s); s++)//转换成十进制
		{
			digit=0;
			if (isdigit(*s))
				digit=*s-'0';
			else
				digit=toupper(*s)-'A'+10;
			dec_sum=dec_sum*16+digit;
		}
		
		if (*s!=0)//如果没有转换到字符串结尾就结束了,就证明在转换的过程中出现了错误(如:z不是合法字符)。
		{
			error=1;
			MessageBox("请检查输入数据!","错误");
		}
		else
		{
			;//ultoa(dec_sum,hex_str,10);
			//MessageBox(hex_str,"十进制输出");
		}
	}
	else
	{
		error=1;
		MessageBox("输入数据不得为空!","错误");
	}
	//数据验证转换完毕
	
	if(m_item_text.GetWindowTextLength()==0)
	{
		MessageBox("[ITEM TEXT]不得为空!","错误");
		error=1;
	}
	else
		m_item_text.GetWindowText(str_item_text,33);
	
	if(error==0)//如果在数据验证转换的过程中未出现错误(error==0时无错误)
	{
		
		//下面为核心部分:按用户指定的项目名称顺序查找Tree-View控件中的Item.
		
		hwnd=HWND(dec_sum);//得到转换后的数据
		
		GetWindowThreadProcessId(hwnd, &PID);
		
		hProcess=OpenProcess(PROCESS_ALL_ACCESS,false,PID);
		if (!hProcess)
			MessageBox("获取进程句柄操作失败!","错误!");
		else
		{
			ptvitem=(TVITEM*)VirtualAllocEx(hProcess, NULL, sizeof(TVITEM), MEM_COMMIT, PAGE_READWRITE);
			pItem=(char*)VirtualAllocEx(hProcess, NULL, 16, MEM_COMMIT, PAGE_READWRITE);
			
			if (!ptvitem)
				MessageBox("无法分配内存!","错误!");
			else
			{
				MessageBox("本演示程序将按用户指定的项目名称顺序查找。","提示");
				
				tvitem.mask=TVIF_TEXT;
				tvitem.cchTextMax=512;
				tvitem.pszText=pItem;
				
				tvitem.hItem=(HTREEITEM)::SendMessage(hwnd, TVM_GETNEXTITEM,TVGN_ROOT, 0x0);//得到根句柄
				::SendMessage(hwnd, TVM_SELECTITEM,TVGN_CARET, (long)tvitem.hItem);//选中状态
				
				WriteProcessMemory(hProcess, ptvitem, &tvitem, sizeof(TVITEM), NULL);//将设置好的结构插入目标进程
				::SendMessage(hwnd, TVM_GETITEM,0, (LPARAM)ptvitem);//发送TVM_GETITEM消息
				ReadProcessMemory(hProcess, pItem, ItemBuf, 512, NULL);//获取pszText
				//MessageBox(ItemBuf,"ITEM TEXT");
				if( strnicmp( ItemBuf,str_item_text,strlen(str_item_text) ) == 0)
				{
					MessageBox("已经找到!","恭喜");
					Bingo=1;
					tvitem.hItem=(HTREEITEM)0x0;//如果根就是我们要找的目标,那么程序执行到这里就可以结束了。
				}
				
				while((long)tvitem.hItem)
				{
					while(::SendMessage(hwnd, TVM_EXPAND,TVE_EXPAND, (long)tvitem.hItem))//当此项目能展开时
					{
						tvitem.hItem=(HTREEITEM)::SendMessage(hwnd, TVM_GETNEXTITEM,TVGN_NEXTVISIBLE, (long)tvitem.hItem);//选择下一个可见项目
						::SendMessage(hwnd, TVM_SELECTITEM,TVGN_CARET, (long)tvitem.hItem);//选中状态
						WriteProcessMemory(hProcess, ptvitem, &tvitem, sizeof(TVITEM), NULL);//将设置好的结构插入目标进程
						::SendMessage(hwnd, TVM_GETITEM,0, (LPARAM)ptvitem);//发送TVM_GETITEM消息
						ReadProcessMemory(hProcess, pItem, ItemBuf, 512, NULL);//获取pszText
						//MessageBox(ItemBuf,"ITEM TEXT");
						if( strnicmp( ItemBuf,str_item_text,strlen(str_item_text) ) == 0)
						{
							MessageBox("已经找到!","恭喜");
							Bingo=1;
							tvitem.hItem=(HTREEITEM)0x0;//如果发现我们要找的目标,那么程序执行到这里就可以结束了。
							break;
						}
						continue;
					}
					
					if(Bingo!=1)
					{
						tvitem.hItem=(HTREEITEM)::SendMessage(hwnd, TVM_GETNEXTITEM,TVGN_NEXTVISIBLE, (long)tvitem.hItem);//当不能再展开的时候,选择下一个可见项目
						::SendMessage(hwnd, TVM_SELECTITEM,TVGN_CARET, (long)tvitem.hItem);//选中状态
						
						WriteProcessMemory(hProcess, ptvitem, &tvitem, sizeof(TVITEM), NULL);//将设置好的结构插入目标进程
						::SendMessage(hwnd, TVM_GETITEM,0, (LPARAM)ptvitem);//发送TVM_GETITEM消息
						ReadProcessMemory(hProcess, pItem, ItemBuf, 512, NULL);//获取pszText
						//MessageBox(ItemBuf,"ITEM TEXT");
						if( strnicmp( ItemBuf,str_item_text,strlen(str_item_text) ) == 0)
						{
							MessageBox("已经找到!","恭喜");
							Bingo=1;
							tvitem.hItem=(HTREEITEM)0x0;//如果发现我们要找的目标,那么程序执行到这里就可以结束了。
							break;
						}
					}
					
				}
			}
		}
	}
	
	//释放内存
	CloseHandle(hwnd);
	CloseHandle(hProcess);
	VirtualFreeEx(hProcess, ptvitem, 0, MEM_RELEASE);
	//顺序查找完毕
	
	if(Bingo==0)
		MessageBox("查找完毕,但并没有发现目标……","很遗憾");
}

void CTV_Demo_SeqSearchDlg::OnAbout() 
{
	// TODO: Add your control notification handler code here
	CAboutDlg dlg;
	dlg.DoModal ();
}